<?php
require_once('src/Utils.php');
require_once('src/vo/PlayerStatsVO.php');
require_once('src/vo/GameStatsVO.php');
require_once('src/vo/TotalGameStatsVO.php');
require_once('src/vo/KillratioStatsVO.php');
 
class Stats {
  
  public function parseStats($events, $players, $flags) {
     
     $stats = new GameStatsVO();
     $stats->init($players, $flags);

     foreach($events as $event) {
        $playerName = Utils::findPlayerName($players, $event->player_id);
        $targetName = Utils::findPlayerName($players, $event->target_id);
        $flagName = Utils::findFlagName($flags, $event->flag_id);

        // suicide
        if ($event->event_type_id == 8) { 
          $stats->players[$playerName]->suicides += 1; 
        }
        // kill        
        if ($event->event_type_id == 3) { 
          $stats->players[$playerName]->kills += 1;
          $stats->players[$targetName]->deaths += 1;
          $stats->players[$playerName]->playerStatsKilled[$targetName] += 1;
          $stats->players[$targetName]->playerStatsLost[$playerName] += 1;
          $stats->players[$playerName]->flagStatsKilled[$flagName] += 1;
          $stats->players[$targetName]->flagStatsLost[$flagName] += 1;  
        }       
     }   
     
     // calculate efficiency stats
     // pick winner (count number of won games)
     $winner = new PlayerStatsVO();     
     foreach($stats->players as $stat) {
        $stat->games = 1;        
        $stat->score = $stat->kills - ($stat->deaths + $stat->suicides);        
        
        if ( ($stat->deaths + $stat->suicides) != 0) {
          $stat->killratio = $stat->kills / ($stat->deaths + $stat->suicides);
        }
        else {
          $stat->killratio = 0;
        }
        
        if ( ($stat->kills) != 0) {
          $stat->power = ($stat->score / $stat->kills) * abs($stat->score / 2);
        }
        else {
          $stat->power = 0;
        }
 
        if ( ($stat->score) > ($winner->score) ) {
            $winner = $stat;
        }
        else if (($stat->score) == ($winner->score)) {
          if (($stat->killratio) > ($winner->killratio)) {
            $winner = $stat;
          }
        }                  
      }     
      
     $winner->wins = 1;

     // sort by killratio
     uasort($stats->players, array($this, "compareKillratio"));
    
     return $stats;
 
  }
  
  public function parseKillratioGraphData($events, $players, $flags, $useSkipCount) {  
     $stats = new GameStatsVO();
     $stats->init($players, $flags);
     
     $killratios = new KillratioStatsVO();
     $killratios->init($players);

     // for graph data, thousands of events serving as datapoints is probably a little overkill
     // lets decrease that to no more than 500 datapoints           
     $skipCountRate = 1;
     if ($useSkipCount == TRUE) {      
       if (count($events) > 500) {
          $skipCountRate = ceil(count($events) / 500);
       }
     }

     $skipCount = array();
     foreach($players as $player) {
        $skipCount[$player->name] = 0;
     }     
  
     foreach($events as $event) {
        $playerName = Utils::findPlayerName($players, $event->player_id);
        $targetName = Utils::findPlayerName($players, $event->target_id);

        // suicide
        if ($event->event_type_id == 8) { 
          $stats->players[$playerName]->suicides += 1; 
        }
        // kill        
        if ($event->event_type_id == 3) { 
          $stats->players[$playerName]->kills += 1;
          $stats->players[$targetName]->deaths += 1; 
        }   
        
        foreach($players as $player) {
            $killratio = 0;
            $playerStat = $stats->players[$player->name];               
            if ( ($playerStat->deaths + $playerStat->suicides) != 0) {
              $killratio = $playerStat->kills / ($playerStat->deaths + $playerStat->suicides);
            }
            else {
              $killratio = 0;
            }
                    
            $skipCount[$player->name]++;
            if ($skipCount[$player->name] == $skipCountRate) {
              array_push($killratios->players[$player->name], $killratio);
              $skipCount[$player->name] = 0;
            }
        }                    
     } 
     
     return $killratios;  
  }
  
  public function aggregateStats($gameStats, $allPlayers, $allFlags) {
       
       $stats = new TotalGameStatsVO();     
       $stats->init($allPlayers, $allFlags); 
          
       foreach($gameStats as $gameStat) {
           foreach($gameStat->players as $playerStat) {    
              $localPlayerStat = $stats->players[$playerStat->name];
              $localPlayerStat->kills += $playerStat->kills;
              $localPlayerStat->deaths += $playerStat->deaths;
              $localPlayerStat->suicides += $playerStat->suicides;
              $localPlayerStat->wins += $playerStat->wins;
              $localPlayerStat->games += $playerStat->games;  
              
              foreach($playerStat->playerStatsKilled as $key => $playerKilled) {
                  $localPlayerStat->playerStatsKilled[$key] += $playerKilled;              
              }   
              foreach($playerStat->playerStatsLost as $key => $playerLost) {
                  $localPlayerStat->playerStatsLost[$key] += $playerLost;              
              } 
              foreach($playerStat->flagStatsKilled as $key => $flagKilled) {
                  $localPlayerStat->flagStatsKilled[$key] += $flagKilled;              
              }   
              foreach($playerStat->flagStatsLost as $key => $flagLost) {
                  $localPlayerStat->flagStatsLost[$key] += $flagLost;              
              }                                                
            
          }        
       }
       
       foreach($stats->players as $stat) {
          $stat->score = $stat->kills - ($stat->deaths + $stat->suicides);        
          
          if ( ($stat->deaths + $stat->suicides) != 0) {
            $stat->killratio = $stat->kills / ($stat->deaths + $stat->suicides);
          }
          else {
            $stat->killratio = 0;
          }
          
          if ( ($stat->kills) != 0) {
            $stat->power = ($stat->score / $stat->kills) * abs($stat->score / 2);
          }
          else {
            $stat->power = 0;
          }                
        } 
        
       // sort by killratio
       uasort($stats->players, array($this, "compareKillratio"));        
  
       return $stats;
  }
    
  public function compareKillratio($a, $b) {
       if ($a->killratio == $b->killratio) {
           return 0;
       }
       else {
           return ($a->killratio > $b->killratio) ? -1 : 1;
       }         
  }              
}
?>
